/*
 * TelaChave.java
 *
 * Created on __DATE__, __TIME__
 */

package telas.comum;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JOptionPane;
import business.seguranca.Sistema;
import business.sistema.ExceptionSistema;

/**
 *
 * @author  __USER__
 */
public class TelaChave extends TelaDialogo
{
	private static final long serialVersionUID = -1132231834652906010L;

	/** Creates new form TelaChave */
	public TelaChave(java.awt.Frame parent, boolean modal)
	{
		super(parent, modal);
		initComponents();
		centraliza();
		setVisible(true);
	}
	
	public void centraliza()  
	{  
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
		Dimension frameSize = this.getSize();  
		if(frameSize.height > screenSize.height)  
			frameSize.height = screenSize.height;  
		if(frameSize.width > screenSize.width)  
			frameSize.width = screenSize.width;  
		this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);  
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents()
	{

		rotulo1 = new telas.componentes.Rotulo();
		ctChave = new telas.componentes.CampoTexto();
		btCancelar = new telas.componentes.Botao();
		btValidar = new telas.componentes.Botao();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Chave de acesso ao sistema.");
		setBounds(new java.awt.Rectangle(0, 0, 0, 0));
		setLocationRelativeTo(null);
		setModal(true);
		setResizable(false);

		rotulo1.setText("Informe a chave:");

		ctChave.setMaxLength(100);

		btCancelar.setText("Cancelar");
		btCancelar.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				btCancelarActionPerformed(evt);
			}
		});

		btValidar.setText("Valida");
		btValidar.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				btValidarActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																ctChave,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																464,
																Short.MAX_VALUE)
														.addComponent(
																rotulo1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																layout
																		.createSequentialGroup()
																		.addComponent(
																				btValidar,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				98,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				btCancelar,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				101,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												rotulo1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												ctChave,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(18, 18, 18)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																btCancelar,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																btValidar,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void btCancelarActionPerformed(java.awt.event.ActionEvent evt)
	{
		try
		{
			dispose();
		} catch (Exception ex)
		{
			new ExceptionSistema(ex);
		}
	}

	private void btValidarActionPerformed(java.awt.event.ActionEvent evt)
	{
		try
		{
			valida();
		} catch (Exception ex)
		{
			new ExceptionSistema(ex);
		}
	}

	private void valida() throws Exception
	{
		String chave = ctChave.getText();

		Sistema sistema = (Sistema) new Sistema().obterObjeto(0);
		
		if(sistema == null)
		{
			sistema = new Sistema();
			
			sistema.setCodigo(0);
		}

		sistema.setChave(chave);

		if (!sistema.isExpirado())
		{
			JOptionPane.showMessageDialog(null,
					"Chave aceita! \n\nObrigado pela preferência!");

//			sistema.salvarSemConfirmacao();

			dispose();
		}
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private telas.componentes.Botao btCancelar;
	private telas.componentes.Botao btValidar;
	private telas.componentes.CampoTexto ctChave;
	private telas.componentes.Rotulo rotulo1;
	// End of variables declaration//GEN-END:variables

}